//
//  FileItemOperation.m
//  OfficeEtc
//
//  Created by  on 12-6-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FileItemOperation.h"

@implementation FileItemOperation
-(id) init
{
    if ((self = [super init])) {
        p_FileManager = [[NSFileManager defaultManager] retain];
    }
    return self;
}

-(void) dealloc
{
    [p_FileManager release];
    [super dealloc];
}

#pragma mark-
#pragma mark files
/**
 @discussion 获得当前路径下所有item 的属性
 @param f_CurrentPath 扫描的路径
 @return 每个文件的属性 （NSDictionary）
*/
-(NSArray*) getCurrentFloderList:(NSString*) f_CurrentPath
{
    NSMutableArray* tItemsArr = [NSMutableArray array];
    NSError* tError = nil;
    NSArray* tItems =[p_FileManager contentsOfDirectoryAtPath:f_CurrentPath error:&tError]; 
    if (tError == nil) {
        NSLog(@"%@",[tItems description]);
        for (NSString* tItemName in tItems) {
            NSMutableDictionary* tItemDic = [NSMutableDictionary dictionary];
            NSString* tPath = [f_CurrentPath stringByAppendingPathComponent:tItemName];
            [tItemDic setValue:tPath forKey:ItemDic_Path];
            NSString* tExtension = [tPath pathExtension];
            NSLog(@"tExtension = %@",tExtension);
            NSDictionary* tAttItemDic = [p_FileManager attributesOfItemAtPath:tPath error:&tError];
            if (tError != nil) {
                NSLog(@"getCurrentFloderList att error =%@",[tError description]);
                [tError release];
                return nil;
            }else {
                [tItemDic setValue:[tAttItemDic objectForKey:@"NSFileModificationDate"] forKey:ItemDic_ModDate];
                [tItemDic setValue:tExtension forKey:ItemDic_Type];
                [tItemDic setValue:tItemName forKey:ItemDic_Name];
            }
            
            if ([tExtension length] <= 0 && ![tItemName isEqualToString:@".DS_Store"]) {//非文件夹 ItemDic_Count
                NSArray* tSubArr = [p_FileManager contentsOfDirectoryAtPath:tPath error:&tError];
                if (tError == nil) {
                    [tItemDic setValue:[NSNumber numberWithInt:[tSubArr count]] forKey:ItemDic_Count];
                }else {
                    NSLog(@"getCurrentFloderList tSubArr error =%@",[tError description]);
                    [tError release];
                    return nil;
                }
            }else {//文件 ItemDic_Size
                [tItemDic setValue:[tAttItemDic objectForKey:@"NSFileSize"] forKey:ItemDic_Size];
            }
            NSLog(@"%@",[tItemDic description]);
            [tItemsArr addObject:tItemDic];
        }
        return tItemsArr;
    }else {
        NSLog(@"getCurrentFloderList error =%@",[tError description]);
        return nil;
    }
}

/**
 @discussion 复制某文件或者文件夹 到 目标路径
 @param f_SourceItem 被复制者路径
 @param f_destinationItem 目标路径
 @return 操作是否成功
 */
- (BOOL) copyItmeAtPath:(NSString *) f_SourceItem
    toDestinationItemAtPath:(NSString*) f_destinationItem
{ 
    f_destinationItem = [f_destinationItem stringByAppendingPathComponent:[f_SourceItem lastPathComponent]];
    
    NSError * tError = nil; 
    if ([ p_FileManager fileExistsAtPath:f_destinationItem])
    {
        if (![p_FileManager removeItemAtPath:f_destinationItem error:&tError])
        {
            NSLog(@"Could not remove old files. Error:%@",tError);
            [tError release];
            return NO;
        }
    }
    tError = nil;
    if ( !( [ p_FileManager copyItemAtPath:f_SourceItem toPath:f_destinationItem error:&tError ]) )
    {
        NSLog(@"Could not copy report at path %@ to path %@. error %@",f_SourceItem, f_destinationItem, tError);
        [tError release];
        return NO;
    }
    return YES;
}

/**
 @discussion 移动某文件或者文件夹 到 目标路径
 @param f_SourceItem 移动者路径
 @param f_destinationItem 目标路径
 @return 操作是否成功
 */
- (BOOL) moveItmeAtPath:(NSString *)f_SourceItem
toDestinationItemAtPath:(NSString*)f_destinationItem
{ 
    f_destinationItem = [f_destinationItem stringByAppendingPathComponent:[f_SourceItem lastPathComponent]];
    
    NSError * tError = nil; 
    if ([ p_FileManager fileExistsAtPath:f_destinationItem])
    {
        if (![p_FileManager removeItemAtPath:f_destinationItem error:&tError])
        {
            NSLog(@"Could not remove old files. Error:%@",tError);
            [tError release];
            return NO;
        }
    }
    tError = nil;
    if ( !( [ p_FileManager moveItemAtPath:f_SourceItem toPath:f_destinationItem error:&tError ]) )
    {
        NSLog(@"Could not copy report at path %@ to path %@. error %@",f_SourceItem, f_destinationItem, tError);
        [tError release];
        return NO;
    }
    return YES;
}

/**
 @discussion 删除某文件或者文件夹下所有内容
 @param f_delItem 删除者路径
 @return 操作是否成功
 */
-(BOOL) deleteItemAtPath:(NSString*) f_delItem
{
    NSError* tError = nil;
    if (![ p_FileManager fileExistsAtPath:f_delItem])
    {
        NSLog(@"delete file item path not exist!!");
        return NO;
    }
    if (![p_FileManager removeItemAtPath:f_delItem error:&tError]) {
        NSLog(@"Could not delete report at path %@. error %@",f_delItem, tError);
        [tError release];
        return NO;
    }
    return YES;
}

@end
